﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Globalization;
using DotNet.Utilities;

namespace AlarmClock
{
    public partial class FrmCalendarTool : Form
    {
        private int locationX;
        private int locationY;
        private bool isLive = true;
        private int model= 0;
        private DateTime dateToday;
        private DateTime currentDate;
        private CnDate cnDate;
        private ChineseCalendar chineseCal;

        private DateTime startDate;

        private Label[]days;
        private DateTime[] dates=new DateTime[35];

        public FrmCalendarTool(int x,int y)
        {
            InitializeComponent();
            locationX = x;
            locationY = y;
        }

        public void setLocation(int left,int top)
        {
            this.Left = left;
            this.Top = top;
        }

        //无边窗体的拖动
        [DllImport("user32.dll")]  //需添加using System.Runtime.InteropServices
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;


        private void CalendarTool_Load(object sender, EventArgs e)
        {
            Screen screen = Screen.PrimaryScreen;
            this.Left = locationX;
            this.Top = locationY;
            this.Region = null;
            SetWindowRegion();

            currentDate=dateToday = DateTime.Now;
            lblDate.Text = string.Format("{0:yyyy/MM}", dateToday);

            days = new Label[] { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10, lbl11, lbl12, lbl13, lbl14, lbl15, lbl16, lbl17, lbl18, lbl19, lbl20, lbl21, lbl22, lbl23, lbl24, lbl25, lbl26, lbl27, lbl28, lbl29, lbl30, lbl31, lbl32, lbl33, lbl34, lbl35 };
            initDays();

            startDate = DateTime.Now;
            timAutoClose_Tick(sender, e);
        }

        private void CalendarTool_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

            startDate = DateTime.Now;
        }

        private DateTime that = new DateTime(2012, 6, 19, 12, 58, 0);

        private void timAutoClose_Tick(object sender, EventArgs e)
        {
            if ((DateTime.Now - startDate).TotalSeconds > 10)
                cmsRetract_Click(sender, e);
        }

        private void timeTimer_Tick(object sender, EventArgs e)
        {     
            DateTime now = DateTime.Now;
            int n = (int)now.DayOfWeek;
            string[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            double time = (now - that).TotalSeconds;
            int year = (int)(time / 31536000);
            int month = (int)((time % 31536000) / 2592000);
            int day = (int)(((time % 31536000) % 2592000) / 86400);
            int hour = (int)((((time % 31536000) % 2592000) % 86400) / 3600);
            int minute = (int)(((((time % 31536000) % 2592000) % 86400) % 3600) / 60);
            int second = (int)(time % 60);
        }

        //绘制圆角
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 2, this.Width, this.Height - 2);//this.Left-10,this.Top-10,this.Width-10,this.Height-10);                 
            FormPath = GetRoundedRectPath(rect, 30);
            this.Region = new Region(FormPath);
        }

        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角   
            path.AddArc(arcRect, 180, 90);
            //   右上角   
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角   
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角   
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }
        
        public bool isAlive()
        {
            return isLive;
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            currentDate=currentDate.AddMonths(-1);
            initDays();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            currentDate = currentDate.AddMonths(1);
            initDays();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            currentDate = currentDate.AddYears(1);
            initDays();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            currentDate = currentDate.AddYears(-1);
            initDays();
        }

        private void MouseOn_lblDays(object sender, EventArgs e)
        {
            startDate = DateTime.Now;
            int i;
            for (i = 0; i < 35; i++)
            {
                if ((Label)sender == days[i])
                    break;
                else continue;
            }
            if (i < 35)
            {
                ttShowDate.ShowAlways = true;
                ttShowDate.SetToolTip((Label)sender, getDateInfo(dates[i]));
            }
        }
        //填充日期列表
        public void initDays()
        {
            DateTime date = currentDate;
            if (model == 0)
            {
                lblDate.Text = string.Format("{0:yyyy/MM}", currentDate);
                while (date.Day > 1)
                {
                    date = date.AddDays(-1);
                }
                int weekOfFirst = (int)date.DayOfWeek;
                DateTime dateb = date;
                //int daysOfThisMonth = 0;
                //while (dateb.Month == date.Month)
                //{
                //    dateb = dateb.AddDays(1);
                //    daysOfThisMonth++;
                //}
                dateb = date.AddDays(-1 * weekOfFirst);
                for (int i = 0; i < 35; i++)
                {
                    dates[i] = dateb;
                    days[i].Text = dateb.Day + "";
                    //当前日期以及上一个月与下一个月日期用不同颜色区分
                    if (dateb.Month != date.Month)
                    {
                        days[i].ForeColor = Color.Brown;
                    }
                    else
                    {
                        days[i].ForeColor = Color.White;
                    }
                    if (dateb == dateToday)
                        days[i].BackColor = Color.Red;
                    else
                        days[i].BackColor = Color.Transparent;
                    dateb = dateb.AddDays(1);
                }
            }
            else
            {
                CnDate todaycnDate = new CnDate(dateToday);
                CnDate curcnDate = new CnDate(currentDate);
                cnDate = new CnDate(currentDate);
                lblDate.Text = (curcnDate.Year + (curcnDate.Month < 10 ? "/0" : "/") + curcnDate.Month);
                while (cnDate.Day > 1)
                {
                    cnDate.AddDays(-1);
                }
                int weekOfFirst = cnDate.DayOfWeek;
                CnDate listcnDate = new CnDate(cnDate);
                listcnDate.AddDays(-1 * weekOfFirst);
                int listMonth,month;
                for (int i = 0; i < 35; i++)
                {
                    dates[i] = listcnDate.getDate();
                    days[i].Text = listcnDate.Day + "";
                    //当前日期以及上一个月与下一个月日期用不同颜色区分
                    listMonth = listcnDate.Month;
                    month = cnDate.Month;
                    if (listcnDate.Month != cnDate.Month)
                    {
                        days[i].ForeColor = Color.Brown;
                    }
                    else
                    {
                        days[i].ForeColor = Color.White;
                    }
                    if (listcnDate == todaycnDate)
                        days[i].BackColor = Color.Red;
                    else
                        days[i].BackColor = Color.Transparent;
                    listcnDate.AddDays(1);
                }
            }
        }

        private void cmsRetract_Click(object sender, EventArgs e)
        {
            //this.SendToBack();
            for (int i = 0; i < Height/2; i++)
            {
                Thread.Sleep(1);
                this.Top -= 2;
            }
            isLive = false;
            this.Close();
        }

        private void btnModel_Click(object sender, EventArgs e)
        {
            if (model == 0)
            {
                model = 1;
                btnModel.Text = "阴";
            }
            else
            {
                model = 0;
                btnModel.Text = "阳";
            }
            initDays();
        }

        public String getDateInfo(DateTime date)
        {
            chineseCal = new ChineseCalendar(date);
            StringBuilder dayInfo = new StringBuilder();
            dayInfo.Append("阳历：" + chineseCal.DateString + "\r\n");
            dayInfo.Append("农历：" + chineseCal.ChineseDateString + "\r\n");
            dayInfo.Append("星期：" + chineseCal.WeekDayStr + "\r\n");
            dayInfo.Append("时辰：" + chineseCal.ChineseHour + "\r\n");
            dayInfo.Append("属相：" + chineseCal.AnimalString + "\r\n");
            dayInfo.Append("节气：" + chineseCal.ChineseTwentyFourDay + "\r\n");
            dayInfo.Append("前一个节气：" + chineseCal.ChineseTwentyFourPrevDay + "\r\n");
            dayInfo.Append("下一个节气：" + chineseCal.ChineseTwentyFourNextDay + "\r\n");
            dayInfo.Append("节日：" + chineseCal.DateHoliday + "\r\n");
            dayInfo.Append("干支：" + chineseCal.GanZhiDateString + "\r\n");
            dayInfo.Append("星宿：" + chineseCal.ChineseConstellation + "\r\n");
            dayInfo.Append("星座：" + chineseCal.Constellation + "\r\n");
            return dayInfo.ToString();
        }
    }
}
